package com.sunrise.estoque.repositorio;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sunrise.estoque.repositorio.iface.NotaFiscalDao;
import com.sunrise.generico.acesso.DaoNegocioService;
import com.sunrise.generico.entidade.NotaFiscal;

@Repository
public class NotaFiscalDaoImpl extends DaoNegocioService<NotaFiscal>
		implements NotaFiscalDao {

	public List<NotaFiscal> listarOrdenado() {
		List<NotaFiscal> list = listar(NotaFiscal.class);
		Collections.sort(list, new Comparator<NotaFiscal>() {

			@Override
			public int compare(NotaFiscal o1, NotaFiscal o2) {
				return o1.getNumero().compareTo(o2.getNumero());
			}
		});
		return list;
	}

	public NotaFiscal getById(Long id) {
		return getById(id, NotaFiscal.class);
	}

	@Override
	public NotaFiscal buscarPorNumeroSerie(Integer numero, Long serie) {
		Criteria crit = getSession().createCriteria(NotaFiscal.class);
		if(numero != null){
			crit.add(Restrictions.eq("numero", numero));
		}
		if(serie != null){
			crit.add(Restrictions.eq("serie.id", serie));
		}
		return (NotaFiscal) crit.uniqueResult();
	}


}
